package love.contact;

import java.util.Stack;

import android.app.Activity;
import android.app.ActivityGroup;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;

/**
 * @author wangweike
 * @create_date 2011-12-24
 * @update_date 2011-12-24
 */

public class NavigationActivity extends ActivityGroup
{
	static public final String ROOTINTENT="rootIntent";
	private int activityCount;
	private boolean justResume;
	private boolean optionsMenuCreated;
	
	public void onCreate(Bundle icicle)
	{
		super.onCreate(icicle);   
		
		Intent intent=null;
		if(icicle!=null)
		{
			intent=icicle.getParcelable(ROOTINTENT);
		}
		else
		{
			intent=this.getIntent().getParcelableExtra(ROOTINTENT);
		}
		pushActivity(intent);
	}
	
	protected void onResume() 
	{
        super.onResume();
        justResume=true;
	}
	
	@Override
    protected void onPause() 
	{
        super.onPause();
    }

    @Override
    protected void onStop() 
    {
        super.onStop();
    }
	
	public void pushActivity(Intent intent)
	{
		justResume=false;
		optionsMenuCreated=false;
		
		//intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
		//intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
		String activityId=""+activityCount;
		Window w = getLocalActivityManager().startActivity(activityId,intent);
	    View view = w.getDecorView();
	    setContentView(view);
	    
	    activityCount++;
	}
	
	//POP
	public void popActivity()
	{
		String activityId=""+(activityCount-1);
		getLocalActivityManager().destroyActivity(activityId, true);
		activityCount--;

		if(activityCount>0)
		{
			activityId=""+(activityCount-1);
			View view=getLocalActivityManager().getActivity(activityId).getWindow().getDecorView();
			setContentView(view);
		}
	}
	
	public void onBackPressed() 
	{
		popActivity();
		
		if(activityCount<=0)
		{
			super.onBackPressed();
		}
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) 
	{		
		MenuItem menuItem=menu.add(0,0,0,"menuNav");
		//menuItem.setIcon(R.drawable.ic_menu_add);

		if(isAbleToCallSub())
		{
			getLocalActivityManager().getCurrentActivity().onCreateOptionsMenu(menu);
		}
		
		optionsMenuCreated=true;
		
		return super.onCreateOptionsMenu(menu);
	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item)
	{
		if(isAbleToCallSub())
		{
			getLocalActivityManager().getCurrentActivity().onOptionsItemSelected(item);
		}
		
		return super.onOptionsItemSelected(item);
	}
	
	public int getActivityCount()
	{
		return activityCount;
	}
	
	public boolean isAbleToCallNav()
	{
		if(activityCount==1 || justResume)
		{
			return true;
		}
		
		return false;
	}
	
	private boolean isAbleToCallSub()
	{
		if(activityCount>1 && !justResume)
		{
			return true;
		}
		
		return false;
	}
	
	@Override
	public boolean onKeyUp(int keyCode, KeyEvent event)
	{
	 
		if(keyCode==KeyEvent.KEYCODE_MENU) 
		{
			 Log.d("NavigationActivity","onKeyUp() KEYCODE_MENU");
			 if(!optionsMenuCreated && getLocalActivityManager().getCurrentActivity()!=null)
			 {
				 getLocalActivityManager().getCurrentActivity().openOptionsMenu();
			 }
		}
		else if(keyCode == KeyEvent.KEYCODE_BACK)
		{
			Log.d("NavigationActivity","onKeyUp() KEYCODE_BACK");
			//finish();
		}

		return super.onKeyUp(keyCode, event);
	 }
}
